/**
 * @author Alejandro
 */

package com.biblioteca.servlets;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import biblioteca.Ejemplar;
import biblioteca.Prestamo;
import biblioteca.Recurso;
import biblioteca.Socio;

/**
 * Servlet implementation class RecursosServlet
 * 
 */
@WebServlet("/adminPrestamos.servlet")
public class PrestamosServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @author Alejandro
     * @see HttpServlet#HttpServlet()
     */
    public PrestamosServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	@SuppressWarnings("unchecked")
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		String accion = request.getParameter("accion");
		System.out.println("Accion: " + accion);
		
		List<Prestamo> listaP = (List<Prestamo>)request.getSession().getAttribute("listaPrestamo");
		Prestamo prestamo = new Prestamo();
		Ejemplar ejemplar = new Ejemplar();

		prestamo.setIdSocio(request.getParameter("idsocio"));
		prestamo.setId(Integer.parseInt(request.getParameter("id")));
		
		//definimos las funciones al accionar los botones
		//guarda la lista si el titulo coincide
		if (accion.equals("guardar")) {
			listaP.add(prestamo);
			response.sendRedirect("prestamos.jsp");
			
		} else if(accion.equals("eliminar")){
			//elimina la lista si el titulo coincide
			Iterator<Prestamo> iter = listaP.iterator();
			while(iter.hasNext()){
			Prestamo list1 = iter.next();
			if(list1.getIdSocio().equals(prestamo.getIdSocio())){
				listaP.remove(list1);
				break; }
		}
			response.sendRedirect("prestamos.jsp");
			}		
		
	}

}